/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function () {

    $(".input-group.date").datepicker({
        todayBtn: true,
        todayHighlight: true,
        keyboardNavigation: false,
        forceParse: false,
        calendarWeeks: true,
        autoclose: true,
        format: "yyyy-mm-dd",
        language: "cn",
    });

    var ue = UE.getEditor('editor', {initialFrameHeight: 250});

    //上传
    $('.upload-img-attachment').ajaxUpload({
        url: '/upfile',
        name:'increase_attachment',
        data: {top: 620},
        onSubmit:function (data){
            $('#text-attachment').html('正在上传中...');
            return true;
        },
        onComplete:function (json){
            var info = json.body.info[0];
            if(json.status==200){
                $('.upload-img-attachment>img').attr('src', '/img/attachment.jpg');
                $('.increase-attachment').val('/files/' + info.savename);
                $('#text-attachment').html('');
            }else{
                alert(json.message);
            }
        }
    });

    $('.btn-save').on('click', function(){
        $('.increase-content').val(ue.getContent());
        var data = {
            title: $('.increase-title').val(),
            created_at: $('.increase-create-time').val(),
            content: $('.increase-content').val(),
            attachment: $('.increase-attachment').val()
        };

        if (checkForm(data)) {
            saveData('/v1/user/save/increase/score', {params:data}, '/user/increase.html');
        }
    });
});

function checkForm(data) {
    if (data.title == '') {
        alertTip('标题');
        return false;
    }
    if (data.created_at == '') {
        alertTip('发布时间不能为空');
        return false;
    }
    if (data.content == '') {
        alertTip('内容不能为空');
        return false;
    }
    return true;
}